package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.vo.WebCourseQueryVo;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author liujun
 * @since 2021-01-20
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private TeacherService teacherService;
    @Override
    public List<Course> webSelectList(WebCourseQueryVo webCourseQueryVo) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        //查询已发布的课程
        queryWrapper.eq("status", Course.COURSE_NORMAL);
        if (!StringUtils.isEmpty(webCourseQueryVo.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", webCourseQueryVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(webCourseQueryVo.getSubjectId())) {
            queryWrapper.eq("subject_id", webCourseQueryVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(webCourseQueryVo.getBuyCountSort())) {
            if(StringUtils.equals(webCourseQueryVo.getBuyCountSort(),"1")){
                queryWrapper.orderByAsc("buy_count");
            }else {
                queryWrapper.orderByDesc("buy_count");
            }
        }
        if (!StringUtils.isEmpty(webCourseQueryVo.getGmtCreateSort())) {
            if(StringUtils.equals(webCourseQueryVo.getGmtCreateSort(),"1")){
                queryWrapper.orderByAsc("gmt_create");
            }else {
                queryWrapper.orderByDesc("gmt_create");
            }
        }
        if (!StringUtils.isEmpty(webCourseQueryVo.getPriceSort())) {
            if(StringUtils.equals(webCourseQueryVo.getPriceSort(),"1")){
                queryWrapper.orderByAsc("price");
            }else {
                queryWrapper.orderByDesc("price");
            }
        }
        return baseMapper.selectList(queryWrapper);

    }

    @Override
    @Cacheable(value = "index", key = "'selectHotCourse'" )
    public List<Course> selectHotCourse() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("view_count");
        queryWrapper.last("limit 8");
        return baseMapper.selectList(queryWrapper);

    }

    @Override
    public CourseDto getCourseDtoById(String courseId) {
        //先获取couse
        final Course course = this.getById(courseId);
        final CourseDto courseDto = new CourseDto(course.getId(),course.getTitle(),course.getPrice(),course.getCover());
        //获取teachername
        final Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher!=null){
            courseDto.setTeacherName(teacher.getName());
        }
        return courseDto;
    }

}
